package com.easonji;

import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.toolkit.StringUtils;

import java.util.Scanner;

/**
 * @ClassName CodeGenerator
 * @Author easonji
 * @Date 2018/9/21 下午3:50
 **/
public class CodeGenerator {

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    private static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        String tablePrefix = "";

        String moduleNameString =scanner("模块名");//scanner("模块名");

        String tableNameString = scanner("表名");//scanner("表名");

        String[] tables = tableNameString.split("_");
        if(tables.length>1){
            tablePrefix = tables[0]+"_";
        }


        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/attendance-admin/src/main/java");
        gc.setAuthor("easonji");
        gc.setOpen(false);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/attendance?useUnicode=true&useSSL=false&characterEncoding=utf8");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(moduleNameString);
        pc.setParent("com.easonji");
        pc.setController("controller");
        pc.setEntity("model");
        mpg.setPackageInfo(pc);


//        TemplateConfig tc = new TemplateConfig();
//        tc.


        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperEntityClass("com.baomidou.mybatisplus.activerecord.Model");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setSuperControllerClass("com.easonji.common.base.controller.BaseController");
        strategy.setSuperServiceImplClass("com.easonji.common.base.service.AbstractBaseService");
        strategy.setInclude(tableNameString);
        strategy.setSuperEntityColumns("id");
        strategy.setTablePrefix(tablePrefix);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setEntityLombokModel(false);
        //strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}
